<?php

namespace common\models;


use yii\db\ActiveRecord;
class SupplierGoods extends ActiveRecord
{
    public static function tableName()
    {
        return 'supplier_goods';
    }

    public function rules()
    {
        return [
            [['goods_name','key','list_img','introduction','content'],'string'],
            [['three_tree_id','two_tree_id','one_tree_id','created_at','type','updated_at',
                'is_del','sales_volume','display_sales','goods_type'],'integer'],
            [['freight','max_price','min_price'],'double']
        ];

    }

    public function attributeLabels()
    {
        return [
            'id	'=> 'id',
            'goods_name'=> '商品名称',
            'tree_id'=> '分类id',
            'type'=> '商品类型',
            'key'=> '搜索关键字',
            'list_img'=>'列表图',
            'introduction	'=> '简介',
            'content'=> '详细介绍',
            'freight	'=> '创建时间',
            'created_at	'=> '创建时间',
            'updated_at'=>'修改时间',
            'is_del'=>'修改时间',
            'sales_volume'=>'销量',
            'display_sales'=>'已团销量',
            'goods_type'=>'商品类型',
            'max_price'=>'原价最高价',
            'min_price'=>'原价最低价',
        ];
    }


    /**
     * @param $data
     * @param int $type 1：总后台
     * @return mixed
     */
    public  function GetgoogsList($data){
        $goodsIdarr = array_column($data,'id');

        $pre = SupplierGoodsAttr::find()->select(['goods_id,max(original_price) as max_pre', 'min(original_price) as min_pre'])
            ->andwhere(['in','goods_id',$goodsIdarr])
            ->groupBy('goods_id')
            ->orderBy(['goods_id'=>SORT_DESC])
            ->asArray()
            ->all();
        $newpre = [];

        foreach ($pre as $jg){
            $newpre[$jg['goods_id']] = $jg;
        }

        foreach ($data as &$val){
            if(isset($newpre[$val['id']])){
                $val['org_pre'] = $newpre[$val['id']]['min_pre'] . '~' . $newpre[$val['id']]['max_pre'];
            } else {
                $val['org_pre'] = '0~0';
            }
        }
        return $data;
    }

}